#include "win_process.h"

void create_a_new_process(_TCHAR *command, bool block) {
  STARTUPINFO si;
  PROCESS_INFORMATION pi;

  ZeroMemory(&si, sizeof(si));
  si.cb = sizeof(si);
  ZeroMemory(&pi, sizeof(pi));

  if (command == 0)
    return;

  // Start the child process.
  if (CreateProcess(0, // No module name (use command line)
    command, // Command line
    0,       // Process handle not inheritable
    0,       // Thread handle not inheritable
    FALSE,   // Set handle inheritance to FALSE
    0,       // No creation flags
    0,       // Use parent's environment block
    0,       // Use parent's starting directory
    &si,     // Pointer to STARTUPINFO structure
    &pi)     // Pointer to PROCESS_INFORMATION structure
    == 0) {
    // cerr << "CreateProcess failed " << GetLastError() << endl;
    return;
  }

  if (block == true) {
    // Wait until child process exits.
    WaitForSingleObject(pi.hProcess, INFINITE);
  }

  // Close process and thread handles.
  CloseHandle(pi.hProcess);
  CloseHandle(pi.hThread);
}
